<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\db;

class CouponModel extends Model
{
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $where['agency_id']=$id;
        $session=DB::name('shop')->where($where)->find();
        return $session;
    }
   /*优惠券列表*/
   public function index($cause)
   {
       $session=$this->session();
       $where['co.shop_id']=$session['id'];
       if(isset($cause['nick_name']))
       {
           if($cause['nick_name'])
           {
               $where['me.nick_name']=array('like','%'.$cause['nick_name'].'%');
           }
       }
       if(isset($cause['voucher_memberid']))
       {
           if($cause['voucher_memberid'])
           {
               $where['co.voucher_memberid']=$cause['voucher_memberid'];
           }
       }
       $data=DB::name('coupon')->alias('co')->join('member me','co.voucher_memberid = me.id')->join('cou c','c.id=co.voucher_id')->where($where)->field('co.*,me.nick_name,c.cause,c.money,c.s_time,e_time')->paginate(10);
       if($data->toArray())
       {
         foreach ($data as $k=>$v)
         {
             $data->toArray();
             $date=array();
             $date=$v;
             if($v['voucher_use']==1){
                 $date['voucher_name']='使用';
             }else{
                 $date['voucher_name']='未使用';
             }
             $date['voucher']='满'.$v['cause'].'减'.$v['money'];
             $data->offsetSet($k,$date);
         }
       }
       $data->appends($cause);
       return $data;

   }
   /*
    * 添加优惠券
    * */
   public function add($data)
   {
       $session=$this->session();
       $date='';
       if(isset($data['userid']))
       {
           $userid=explode(',',$data['userid']);
           foreach ($userid as $k=>$v)
           {
               if($v)
               {
                   $array['cou_guid']=guid();
                   $array['voucher_memberid']=$v;
                   $array['voucher_id']=$data['coupon'];
                   $array['shop_id']=$session['id'];
                   $date[$k]=$array;
               }
           }

       }
       $status=Db::name('coupon')->insertAll($date);
       return $status;
   }
   /**
    * 根据id获取列表记录
   */
   public function getcone($where){
       $session=$this->session();
       $where['shop_id']=$session['id'];
       $status= DB::name('cou')->where($where)->select();
       return $status;
   }
   /*获取会员*/
   public function user($array,$status)
   {
       $session=$this->session();
       $where['shop_id']=$session['id'];
       if(isset($array['nick_name']))
       {
          if($array['nick_name'])
          {
              $where['nick_name']=array('like','%'.$array['nick_name'].'%');
          }
       }
       if(isset($array['id']))
       {
           if($array['id'])
           {
               $where['id']=$array['id'];
           }
       }
       if($status==1)
       {
           $user=DB::name('member')->where($where)->paginate(10);
           $user->appends($where);
       }else{
           $user=DB::name('member')->where($where)->find();
       }
       return $user;
   }

}